package com.example.communitypro.controller;

import com.example.communitypro.pojo.*;
import com.example.communitypro.service.NotificationService;
import com.example.communitypro.service.UserCollectionService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/userCollection")
public class UserCollectionController {
    @Autowired
    private UserCollectionService userCollectionService;
    @Autowired
    private NotificationService notificationService;



    @ResponseBody
    @RequestMapping(value = "/addUserCollection")
    @CrossOrigin
    public Result addUserCollection(UserCollection record){
        Notification notification1 = new Notification();
        notification1.setNotificationDate(new Date());

        notification1.setNotificationType(2);
        notification1.setPostNo(record.getCollectionObjectNo());
        notification1.setUserNo(record.getUserNo());
        notificationService.addNotification(notification1);
        return userCollectionService.addUserCollection(record);
    }
    @ResponseBody
    @RequestMapping(value = "/dropUserCollection")
    @CrossOrigin
    public Result dropUserCollection(Integer collection_no){
        return userCollectionService.dropUserCollection(collection_no);
    }

    @ResponseBody
    @RequestMapping(value = "/modifyUserCollection")
    @CrossOrigin
    public Result modifyUserCollection(UserCollection record){
        return userCollectionService.modifyUserCollection(record);
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserCollection")
    @CrossOrigin
    public Result queryUserCollection(Integer collection_no){
        return userCollectionService.queryUserCollection(collection_no);
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommunityCollection")
    @CrossOrigin
    public Result queryCommunityCollection(Integer user_no){
        return userCollectionService.queryCommunityCollection(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/queryPostCollection")
    @CrossOrigin
    public Result queryPostCollection(Integer user_no){
        return userCollectionService.queryPostCollection(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommentCollection")
    @CrossOrigin
    public Result queryCommentCollection(Integer user_no){
        return userCollectionService.queryCommentCollection(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/UserCollection")
    @CrossOrigin
    public Result UserCollection(UserCollection record){
        Notification notification1 = new Notification();
        notification1.setNotificationDate(new Date());

        if(record.getCollectionObjectType()==1){
            notification1.setNotificationType(2);
        }else if(record.getCollectionObjectType()==2){
            notification1.setNotificationType(4);
        }
        notification1.setPostNo(record.getCollectionObjectNo());
        notification1.setUserNo(record.getUserNo());
        if (record.getCollectionObjectType()!=0){
            notificationService.addNotification(notification1);
        }
        return userCollectionService.UserCollection(record);
    }

    @ResponseBody
    @RequestMapping(value = "/UserCollectionCommunity")
    @CrossOrigin
    public Result UserCollectionCommunity(Integer userNo,Integer collectionObjectType,Integer collectionObjectNo){
        return userCollectionService.UserCollectionCommunity(userNo,collectionObjectType,collectionObjectNo);
    }
}
